package com.itheima.service;

import com.itheima.pojo.Setmeal;
import freemarker.template.Template;
import org.aspectj.lang.JoinPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class GenerateStaticHtml {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;

    @Value("${out_put_path}")
    private String outPath;

    //生成静态页面
    public void generateStaticHtml(){
        //查询所有套餐信息
        List<Setmeal> setmealList = setmealService.findAll();

        //生成列表页面
        generateSetmealStaticHtml(setmealList);
        //生成详情页面
        generateSetmealDetailStaticHtml(setmealList);
    }


    //生成套餐列表的静态页面
    public void generateSetmealStaticHtml( List<Setmeal> setmealList){

        //将setmealList装到Map集合中
        Map<String,List<Setmeal>> map = new HashMap<>();
        map.put("setmealList",setmealList);
        Writer writer=null;
        try {

            //定义一个输出流
             writer = new FileWriter(outPath + "\\" + "m_setmeal.html");

            //获取模板对象
            Template template = freeMarkerConfigurer.getConfiguration().getTemplate("setmeal.ftl");

            //生成静态页面
            template.process(map, writer);
        }catch (Exception ex){
            ex.printStackTrace();
        }finally{
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }


    //生成套餐详情的静态页面
    public void generateSetmealDetailStaticHtml( List<Setmeal> setmealList){

        for (Setmeal setmeal : setmealList) {
            Writer writer=null;
            try {

                //由于setmeal对象中并没有为checkgroups及checkItems属性赋值
                //此时需要跟据id查询出详情
                Setmeal setmealDetail = setmealService.findDetailById(setmeal.getId());

                //准备数据
                Map map = new HashMap();
                map.put("setmeal", setmealDetail);

                //准备输出流
                writer = new FileWriter(outPath + "\\" + "setmeal_detail_" + setmealDetail.getId() + ".html");

                //获取模板对象
                Template template = freeMarkerConfigurer.getConfiguration().getTemplate("setmeal_detail.ftl");

                //生成静态页面
                template.process(map, writer);
            }catch(Exception ex){
                ex.printStackTrace();
            }finally{
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }

    //根据id删除已经生成静态页面
    public void removeStaticHtmlBySetmealId(JoinPoint jp){


        File file = new File(outPath+"//"+"setmeal_detail_"+jp.getArgs()[0]+".html");
        if(file.exists()){
            file.delete();
        }
    }

}
